/**
 * 
 */
package ar.com.fiuba.team2.parser;

import java.io.IOException;
import java.io.StringReader;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import ar.com.fiuba.team2.model.Chat;

/**
 * @author sergio
 *
 */
public abstract class ChatParser extends GeneralParser{
	
	private static Logger log = LogManager.getLogger(ChatParser.class);

	public static Chat parseChat(String xml) {
		Chat chat = null;
		
		SAXBuilder saxBuilder = new SAXBuilder();
		try {
		    Document doc = saxBuilder.build(new StringReader(xml));
			Element rootElement = doc.getRootElement().getChild("list");
			for (Element chatXML : rootElement.getChildren("Chat")) {
				chat = new Chat();
				chat.fromXML(chatXML);
			}
		} catch (JDOMException e) {
			log.error("error al parsear char de " + xml, e);
		} catch (IOException e) {
			log.error("error al parsear char de " + xml, e);
		} catch (RuntimeException e) {
			log.error("error al parsear char de " + xml, e);
		}
		
		return chat;
	}
}
